home *** CD-ROM | disk | FTP | other *** search
- Path: news.kempele.fi!lyyra!quaid
- From: quaid@lyyra.kempele.fi (Jarkko Vatjus-Anttila)
- Newsgroups: comp.sys.amiga.programmer
- Subject: Re: Randomness
- Date: 20 Apr 1996 15:19:54 GMT
- Organization: Kempeleen lukio
- Message-ID: <4lavaq$i30@louhi.kempele.fi>
- References: <451.6681T677T994@summat.demon.co.uk>
- NNTP-Posting-Host: lyyra.kempele.fi
- NNTP-Posting-User: quaid
- X-Newsreader: TIN [version 1.2 PL2]
-
- Mike Dodd (mike@summat.demon.co.uk) wrote:
- : Help !,
-
- : Has anyone got a good algorithm for quickly calculating a "random" number ?
-
- Well I have. You need a routine to create a seed for rnd numbers and that
- can be easily done *for example* with this routine.
-
- move.l #datestamp,d1 ; space for DateStamp() output. 12 bytes.
- move.l dosbase,a6
- jsr _LVODateStamp(a6)
- move.l d0,a0
- move.l 4(a0),d1
- mulu.w d1,d0
- move.l 4(a0),d1 ; number of tics (1/50th seconds)
- mulu.w d1,d0
- move.w d0,seed ; this is the seed.
-
- The seed has to be calculated only when you start the program. This
- routine uses DateStamp routine to get system time and because it uses the
- number of tics to create the seed, it's hardly never the same. This gives
- you totally random numbers.
-
- Once the seed is initialised you can yse this routine to create rnd numbers:
- Just give the max limit in d0.
-
- rndnumber
- move.w seed,d1
- mulu.w #31421,d1
- add.w #6927,d1
- mulu.w d1,d0
- move.w d1,seed ; init new seed to make sure that numbers remain random
- clr.w d0
- swap d0 ; upper 16 bits are the most random.
- rts
-
- : I've tried the algorithm described in the autodocs for amiga.lib fo
- : "FastRand" :
-
- I once tried to deal with those FastRand and other routines but with no
- success. I think they suck. I don't know if I did somethign wrong when I
- tried to create numbers with 'em, but actually I don't care. My routine
- is capable to create rnd numbers in range 0-65536 and that sure is enough
- to me. The main thing is that I get rnd numbers.
-
- : but this produces a rather poor sequence of values...Soooerh, any better
- : suggestions ?
-
- What do you think of mine?
-
- --
- --------------------------------------------------------------
- |AMiGA Jarkko Vatjus-Anttila (Quaid/BFB Team) POWER|
- |WWW: http://www.kempele.fi/~quaid/ EMail: quaid@kempele.fi|
- --------------------------------------------------------------
- |_| !A1200/030/50Mhz/882/50Mhz/6mb memory/840MB HDD! |_|
-